<script setup lang="ts">
import {
  getHomeBannerAPI, getHomeCategoryAPI, getHomeHotAPI,
} from '@/services/home'
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
//引入组件CustomNavBar
import CustomNavBar from './componets/CustomNavbar.vue'
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
import CategoryPanel from './componets/CategoryPanel.vue'
import HotPanel from './componets/HotPanel.vue'
import type { XtxGuessInstance } from '@/components/components'
// 加载骨架屏
import PageSkeleton from './componets/PageSkeleton.vue'
import { useGuessList } from '@/composables'

// 定义加载gif图变量
const skeletonLoading = ref(true)

// 获取轮播图数据
const bannerList = ref<BannerItem[]>([])
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  bannerList.value = res.result
}
// 获取首页分类数据
const categoryList = ref<CategoryItem[]>([])
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPI()
  categoryList.value = res.result
}
// 获取首页推荐数据
const hotList = ref<HotItem[]>([])
const getHomeHotData = async () => {
  const res = await getHomeHotAPI()
  hotList.value = res.result
}

// 加载骨架屏变量
const isLoading = ref(false)

// 页面加载
onLoad(async () => {
  isLoading.value = true
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHomeHotData()])
  skeletonLoading.value = false
  isLoading.value = false
})
// 获取猜你喜欢组件实例
const { guessRef, onScrollToLower } = useGuessList()

// 当前下拉刷新状态
const isTriggered = ref(false)
// 自定义下拉刷新被触发
const onRefresherrefresh = async () => {
  // 开始动画
  isTriggered.value = true
  skeletonLoading.value = true
  // 重置猜你喜欢组件数据
  guessRef.value?.resetData()
  // 同时请求多个接口, 等待所有接口都返回之后才关闭动画
  await Promise.all([getHomeBannerData(),getHomeCategoryData(),getHomeHotData()])
  // 加载猜你喜欢组件数据
  guessRef.value?.getMore()
  // 关闭动画
  isTriggered.value = false
  skeletonLoading.value = false
}
</script>

<template>
  <!--自定义导航栏-->
  <CustomNavBar />
  <!--滚动容器-->
  <scroll-view
    refresher-enabled
    @refresherrefresh="onRefresherrefresh"
    @scrolltolower="onScrollToLower"
    :refresher-triggered="isTriggered"
    :scroll-y="true"
    class="scroll-view">
    <!--加载骨架屏-->
    <PageSkeleton v-if="isLoading" />
    <!-- 加载中提示 -->
    <view class="lanjiazai_wrap" v-if="skeletonLoading">
      <view class="lanjiazai_item">
        <view class="loading_icon">
          <image src="../../static/images/logo_loading.gif" class="loading_gif"></image>
        </view>
        <view class="tag">加载中...</view>
      </view>
    </view>

    <template v-else>
      <!--自定义轮播图-->
      <XtxSwiper :list="bannerList" />
      <!--分类面板-->
      <CategoryPanel :list="categoryList" />
      <!--热门推荐-->
      <HotPanel :list="hotList" />
      <!--猜你喜欢-->
      <XtxGuess ref="guessRef" />
    </template>
  </scroll-view>
</template>

<style lang="scss">
page {
  height: 100%;
  background-color: $uni-bg-color-grey;
  display: flex;
  flex-direction: column;
}
.scroll-view {
  flex: 1;
}
.lanjiazai_wrap {
  height: calc(100vh);
  text-align: center;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  color: #999;
  padding-top: 100rpx;
  .lanjiazai_item {
    text-align: center;
    .loading_icon {
      width: 120rpx;
      height: 120rpx;
      .loading_gif {
        width: 120rpx;
        height: 120rpx;
      }
    }
    .tag {
      margin-top: 8rpx;
      font-size: 24rpx;
    }
  }
}
</style>
